package com.anolesoft.yr.mbus.fin.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.fin.dto.FinBillingApplyInvoiceDTO;
import com.anolesoft.yr.mbus.fin.service.FinBillingApplyInvoiceService;
import com.anolesoft.yr.mbus.fin.vo.FinBillingApplyInvoiceVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/15 14:40
 */
@RestController
public class FinBillingApplyInvoiceController {
    @Autowired
    private FinBillingApplyInvoiceService finBillingApplyInvoiceService;

    @PostMapping(value = "/api/fin/finbillingapplyinvoice/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(FinBillingApplyInvoiceVO finBillingApplyInvoiceVO) {
        FinBillingApplyInvoiceDTO finBillingApplyInvoideDTO = vo2dto(finBillingApplyInvoiceVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, finBillingApplyInvoiceService.create(finBillingApplyInvoideDTO));
    }

    private FinBillingApplyInvoiceDTO vo2dto(FinBillingApplyInvoiceVO finBillingApplyInvoiceVO, boolean newId) {
        return FinBillingApplyInvoiceDTO.builder()
                .invoiceCode(finBillingApplyInvoiceVO.getInvoiceCode())
                .receiptCode(finBillingApplyInvoiceVO.getReceiptCode())
                .factoryCode(finBillingApplyInvoiceVO.getFactoryCode())
                .remark(finBillingApplyInvoiceVO.getRemark())
                .objuid(newId ? CommonUtils.fastUUID() : finBillingApplyInvoiceVO.getObjuid())
                .modifyUserUid(SecurityUtils.getUserId())
                .deleteFlag(finBillingApplyInvoiceVO.getDeleteFlag())
                .createUserUid(SecurityUtils.getUserId())
                .createTime(finBillingApplyInvoiceVO.getCreateTime())
                .amount(finBillingApplyInvoiceVO.getAmount())
                .finBillingApplyUid(finBillingApplyInvoiceVO.getFinBillingApplyUid())
                .modifyTime(finBillingApplyInvoiceVO.getModifyTime())
                .build();
    }

    @PutMapping(value = "/api/fin/finbillingapplyinvoice/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(FinBillingApplyInvoiceVO finBillingApplyInvoiceVO) {
        FinBillingApplyInvoiceDTO finBillingApplyInvoideDTO = vo2dto(finBillingApplyInvoiceVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, finBillingApplyInvoiceService.modify(finBillingApplyInvoideDTO));
    }

    @DeleteMapping(value = "/api/fin/finbillingapplyinvoice/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, finBillingApplyInvoiceService.remove(id));
    }

    @DeleteMapping(value = "/api/fin/finbillingapplyinvoice/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult removeBatch(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, finBillingApplyInvoiceService.removeBatch(ids));
    }

    @GetMapping(value = "/api/fin/finbillingapplyinvoice/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<FinBillingApplyInvoiceDTO> finBillingApplyInvoideDTO = this.finBillingApplyInvoiceService.selectOneById(id);
        if (finBillingApplyInvoideDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.finBillingApplyInvoiceDTO2VO(finBillingApplyInvoideDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }
    }

    @GetMapping(value = "/api/fin/finbillingapplyinvoice/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<FinBillingApplyInvoiceVO> vos = finBillingApplyInvoiceService.selectAll().stream().map(CustomBeanUtils::finBillingApplyInvoiceDTO2VO).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyInvoiceVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/fin/finbillingapplyinvoice/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
        PageData<List<FinBillingApplyInvoiceDTO>> pageData = finBillingApplyInvoiceService.selectAll(limit, curr);
        List<FinBillingApplyInvoiceVO> vos = pageData.getItems().stream().map(CustomBeanUtils::finBillingApplyInvoiceDTO2VO).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyInvoiceVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/fin/finbillingapplyinvoice/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(FinBillingApplyInvoiceVO finBillingApplyInvoiceVO) {
        FinBillingApplyInvoiceDTO finBillingApplyInvoiceDTO = vo2dto(finBillingApplyInvoiceVO, false);
        List<FinBillingApplyInvoiceVO> vos = this.finBillingApplyInvoiceService.selectList(finBillingApplyInvoiceDTO).stream().map(t -> CustomBeanUtils.finBillingApplyInvoiceDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyInvoiceVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/fin/finbillingapplyinvoice/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageList(FinBillingApplyInvoiceVO finBillingApplyInvoiceVO) {
        FinBillingApplyInvoiceDTO finBillingApplyInvoiceDTO = vo2dto(finBillingApplyInvoiceVO, false);
        Integer limit = finBillingApplyInvoiceVO.getRows();
        Integer curr = finBillingApplyInvoiceVO.getPage();
        PageData<List<FinBillingApplyInvoiceDTO>> pageData = finBillingApplyInvoiceService.selectList(finBillingApplyInvoiceDTO, limit, curr);
        List<FinBillingApplyInvoiceVO> vos = pageData.getItems().stream().map(CustomBeanUtils::finBillingApplyInvoiceDTO2VO).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyInvoiceVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }
}
